<?php
namespace jy_mzhaoshang;
defined('BY_JYA') or exit('error');
/*
	分销插件会员类
	如果将分销商各项数据在每次数据变动时计算好并存入数据库。
	那么分别需要做
		在设置上下级关系时，修改新上级的 下线数量、分销商数量、订单数量、订单佣金数量等数据
		在变更上下级关系时，修改原、新上级的各项参数
		在发生订单时，修改上级的订单相关参数
		在用户成为分销商时，修改上级的分销商数量相关数据
*/
class zhaoshang extends \plugin{
	public function checkmember(){
		if($this->setting['isopen']!=1){
			exi('暂未开通商户店长活动！','error','jy_mzhaoshang/apply/');
		}
		$member=pdo_get('jy_mzhaoshang_list',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid']));
		if(!$member['id']){
			//判断是否满足申请分销商条件
			$can_apply=true;
			if($this->setting['become']==3){
				//检查金额
				$money=pdo_getsum('jy_weishop_order',array('merchid'=>0,'uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'ispay'=>1),'price');
				if($money<$this->setting['goods_fee']){
					$can_apply=false;
					$member['now']=price($money);
					$member['limit']=price($this->setting['goods_fee']);
					$member['unit']='元';
				}
			}
			if($this->setting['become']==4){
				//检查是否购买本商品
				$can_apply=false;
				$become_goods=$this->setting['become_goods'];
				foreach($become_goods as &$bgv){
					$isbuy=pdo_count('jy_weishop_order_goods',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$_SESSION['uid'],'goodsid'=>$bgv['id'],'ispay'=>1));
					if($isbuy>0){
						$can_apply=true;
					}
					$goods=pdo_get('jy_weishop_goods',array('id'=>$bgv['id']),array('id','title','image','price','active_plugin','active_id','salenum','originalprice','usermaxbuy','unit'));
					$bgv=array_merge($goods,$bgv);
				}
				$member['become_goods']=$become_goods;
			}
			$member['can_apply']=$can_apply;
			if($can_apply){
				$member=$this->register();
			}
		}
		if(!$this->setting['become_check'] && !$member['status']){
			$this->update('list',array('status'=>1),array('uniacid'=>$_SESSION['uniacid'],'id'=>$member['id']));
			$member['status']=1;
		}
		return $member;
	}
	/*
		会员注册
	*/
	public function register($member=array()){
		if(!$member['uniacid']){
			$member['uniacid']=$_SESSION['uniacid'];
		}
		if(!$member['mid']){
			$member['mid']=$_SESSION['uid'];
		}
		if(!$member['createtime']){
			$member['createtime']=TIMESTAMP;
		}
		if(!$member['mid']){
			return error('商户店长注册失败，缺少会员ID！');
		}
		$ismid=pdo_get('jy_mzhaoshang_list',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$member['mid']),array('mid'));
		if($ismid){
			exi('您已经是商户店长，请勿重复申请！','error');
		}
		if($this->setting['isopen']!=1){
			exi('暂未开通商户店长活动！','error','jy_mzhaoshang/apply/');
		}
		$member['status']=0;
		if(!$this->setting['become_check']){
			$member['status']=1;
		}
		pdo_insert('jy_mzhaoshang_list',$member);
		$member['id']=pdo_insertid();
		return $member;
	}
	/*
		审核区域代理
	*/
	public function check_agent($agent_id,$status,$remark=''){
		if(!$agent_id || !$status){
			return;
		}
		$agent_area=$this->get('list',array('id'=>$agent_id));
		if(!$agent_area){
			return;
		}
		$update=array(
			'status'=>$status,
			'check_time'=>TIMESTAMP,
		);
		if($update['status']==1){
		}else{
			$update['remark']=$remark;
		}
		$this->update('list',$update,array('id'=>$agent_id));
		return true;
	}
	/*
		禁用代理商
	*/
	public function stop_agent($agent_id){
		$this->update('list',array('status'=>2),array('mid'=>$agent_id));
	}
	/*
		恢复代理商
	*/
	public function start_agent($agent_id){
		$this->update('list',array('status'=>1),array('mid'=>$agent_id));
	}

	/*
		升级分销商等级
		先检测会员身份是否为正常分销商
		然后获取分销商当前等级的下一等级升级条件
		然后逐个条件对比，看是否满足升级条件
		如果全部满足，则升级到相应等级
		
		何时触发升级分销商等级检测呢？
		
	*/
	public function update_agent_level($zhaoshangid){
		$member=$this->get('list',array('id'=>$zhaoshangid));
		if($member['stop']){
			return;
		}
		$level=pdo_getcolumn('jy_merch_level',array('id'=>$member['level_id']),'level');
		$parameter=array(
			'params'=>array('uniacid'=>$_SESSION['uniacid'],'level >'=>$level),
			'orderby'=>'level desc',
		);
		$levels=$this->getall('level',$parameter['params'],'','',$parameter['orderby']);
		if(empty($levels)){
			return;
		}
		$level_up_types=$this->model()->current_level_up_types();//获取当前设置的分销商等级升级条件
		if(!$level_up_types){
			return;
		}
		foreach($levels as $lv){
			$conditions=json_decode($lv['conditions'],true);
			if(!$level_up_types || !$lv['id']){
				break;
			}
			$up=true;
			foreach($level_up_types as $type){
				if(!$this->level_up_types[$type]){
					continue;
				}
				if($type=='buy_goods' && $lv['goods_ids']){
						$goods_ids=json_decode($lv['goods_ids'],true);
						foreach($goods_ids as $gv){
								if($type=='buy_goods'){
									$isbuynum=pdo_getsum('jy_weishop_order_goods',array('ispay'=>1,'mid'=>$member['mid'],'goodsid'=>$gv['id']),'total');
									if($isbuynum>0){
										break;
									}
								}
						}
						if(!$isbuynum){
							$up=false;
							break;
						}
				}else{
					if($member[$type]<$conditions[$type]){
						$up=false;
						break;
					}
				}
			}
			if($up){
				$next_level=$lv;
				break;
			}
		}
		if($up){
			$update=array(
				'level_id'=>$next_level['id'],
			);
			$this->update('list',$update,array('id'=>$zhaoshangid));
		}
		return true;
	}
}